package com.itnoob.mockdata;

import com.itnoob.domain.Request;
import com.itnoob.domain.User;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

// 依赖注入的类，用于模拟从数据库中获取用户信息和请求信息
// 这里使用了@Component注解，让Spring容器管理这个类的实例
@Component
public class DataProvider {
    // 模拟数据库中的用户信息，使用Map存储
    private Map<String, User> users;

    // 模拟数据库中的请求信息，使用Map存储
    private Map<String, Request> requests;

    // 在构造方法中初始化数据
    public DataProvider() {
        users = new HashMap<>();
        requests = new HashMap<>();
        // 创建用户对象，并添加到Map中
        User user1 = new User("admin", "123456", Arrays.asList("admin", "user"), Arrays.asList("read", "write", "delete"));
        User user2 = new User("user1", "123456", Arrays.asList("user"), Arrays.asList("read", "write"));
        User user3 = new User("user2", "123456", Arrays.asList("user"), Arrays.asList("read"));
        users.put(user1.getUsername(), user1);
        users.put(user2.getUsername(), user2);
        users.put(user3.getUsername(), user3);
        // 创建请求对象，并添加到Map中
        Request request1 = new Request("/api/user/list", "GET", Arrays.asList("admin"), Arrays.asList("read"));
        Request request2 = new Request("/api/user/add", "POST", Arrays.asList("admin"), Arrays.asList("write"));
        Request request3 = new Request("/api/user/delete", "DELETE", Arrays.asList("admin"), Arrays.asList("delete"));
        Request request4 = new Request("/api/user/update", "PUT", Arrays.asList("user"), Arrays.asList("write"));
        requests.put(request1.getPath(), request1);
        requests.put(request2.getPath(), request2);
        requests.put(request3.getPath(), request3);
        requests.put(request4.getPath(), request4);
    }

    // 根据用户名获取用户对象
    public User getUserByUsername(String username) {
        return users.get(username);
    }

    // 根据请求路径获取请求对象
    public Request getRequestByPath(String path) {
        return requests.get(path);

    }
}